﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class welcome : System.Web.UI.Page
{
    private string userID;

    protected void Page_Load(object sender, EventArgs e)
    {
        //验证是否登录
        if (Request.Cookies["userID"] == null)
        {
            Response.Redirect("notLogin.html", true);
            return;
        }
        else
        {
            //获取已登录用户信息
            userID = Request.Cookies["userID"].Value;

            ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["connectionstring"];
            using (SqlConnection dataBaseConnection = new SqlConnection(settings.ConnectionString))
            {
                dataBaseConnection.Open();
                //获取待处理的请求列表
                string queryString = "SELECT username, GroupName, RequestList.GroupID, RequestList.userID " +
                                     "FROM users, GroupInfo, RequestList " +
                                     "WHERE CreatedBy = @CreatedBy and GroupInfo.GroupID = RequestList.GroupID and RequestList.userID = users.userID";
                SqlCommand command = new SqlCommand(queryString, dataBaseConnection);
                SqlParameter CreatedByParameter = new SqlParameter("@CreatedBy", userID);
                command.Parameters.Add(CreatedByParameter);
                SqlDataReader reader;
                try
                {
                    reader = command.ExecuteReader();
                }
                catch (Exception exception)
                {
                    Response.Redirect("DBError.aspx?error_msg=" + exception.Message.Replace(' ', '-') + "-AFTER-QUERY-OF-REQUEST", true);
                    return;
                }

                if (!reader.HasRows)
                {
                    requestlistUl.InnerHtml = "<p>暂时没有需要您处理的请求</p>";
                }
                else
                {
                    while (reader.Read())
                    {
                        //todo 这里的处理可能需要更改
                        requestlistUl.InnerHtml += "<li>" + reader["username"] + "申请加入您的小组" + reader["GroupName"] + "<br>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;<a href=\"processRequest.aspx?userID=" + reader["userID"] + "&groupID=" + reader["GroupID"] + "&accepted=1\"><img src=\"images/accepted.png\"></a>  <a href=\"processRequest.aspx?userID=" + reader["userID"] + "&groupID=" + reader["GroupID"] + "&accepted=0\"><img src=\"images/denied.png\"></a></li>";
                    }
                }
                reader.Close();


                //获取该用户加入的小组列表
                queryString = "SELECT GroupIDs " +
                              "FROM users " +
                              "WHERE userID = @userID";
                command.CommandText = queryString;
                command.Parameters.Clear();
                SqlParameter userIDParameter = new SqlParameter("@userID", userID);
                command.Parameters.Add(userIDParameter);
                try
                {
                    reader = command.ExecuteReader();
                }
                catch (Exception exception)
                {
                    Response.Redirect("DBError.aspx?error_msg=" + exception.Message.Replace(' ', '-') + "-AFTER-GET-GROUPLIST", true);
                    return;
                }

                reader.Read();
                string[] groups = reader["GroupIDs"].ToString().Split('&');
                reader.Close();

                bool hasGroup = false;
                int groupNum = groups.Length;
                for (int i = 0; i < groupNum; i++)
                {
                    if (groups[i] != string.Empty)
                    {
                        hasGroup = true;

                        queryString = "SELECT GroupName " +
                                      "FROM GroupInfo " +
                                      "WHERE GroupID = @GroupID";
                        command.CommandText = queryString;
                        command.Parameters.Clear();
                        SqlParameter GroupIDParameter = new SqlParameter("@GroupID", groups[i]);
                        command.Parameters.Add(GroupIDParameter);
                        try
                        {
                            reader = command.ExecuteReader();
                        }
                        catch (Exception exception)
                        {
                            Response.Redirect("DBError.aspx?error_msg=" + exception.Message.Replace(' ', '-'), true);
                            return;
                        }

                        reader.Read();
                        string groupName = reader["GroupName"].ToString();
                        reader.Close();
                        grouplistUl.InnerHtml += "<li><a href=\"groupIndex.aspx?groupID=" + groups[i] + "&groupName=" + groupName + "\">" + groupName + "</a></li>";
                    }
                }

                if (!hasGroup)
                {
                    grouplistUl.InnerHtml = "您还没有加入任何小组";
                }

                dataBaseConnection.Close();
            }

           
        }
        
    }
}